﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Cache;
using System.Runtime.Remoting;
using Imgur.Net.Auth;
using NUnit.Framework;
using TestableHttpWebResponse;

namespace Imgur.Net.Test.ImageProvider
{
    [TestFixture]
    public class CreateAlbumTests
    {
        private const string clientId = "b56c4ebcd84a2c5";
        private const string clientSecret = "5b83ef6e0100a5a8544cc03f2e49c470d7f7cb2f";

        private const string baseTestUriTemplate = "test://api.imgur.com/3/album";

        [TestFixtureSetUp]
        public void TestFixtureSetup()
        {
            WebRequest.RegisterPrefix("test", TestableWebRequestCreateFactory.GetFactory());
        }

        [Test]
        public void TestCreateAlbumBasic()
        {
            ImgurAuthProvider authProvider = new AnonymousAuthenticationProvider(clientId);
            
            AlbumProvider albumProvider = new DefaultAlbumProvider(authProvider, baseTestUriTemplate);

            var expectedRequest = new TestableWebRequest(new Uri(baseTestUriTemplate));
            expectedRequest.EnqueueResponse(HttpStatusCode.OK, "Success", ResourceLoader.GetResourceText("Resources/AlbumProvider/AlbumModel.newcreate.json"), false);
            TestableWebRequestCreateFactory.GetFactory().AddRequest(expectedRequest);
            
            //AlbumProvider albumProvider = new DefaultAlbumProvider(authProvider.GetAuthorizationContext());

            IEnumerable<string> ids = new List<string>() { "MhjVogo", "EN2Tsxg", "n7AuG6d" };

            AlbumInfo info = albumProvider.CreateAlbum(ids, null, null, PrivacyLevel.Public, LayoutStyle.Vertical, null);

            Assert.IsNotNull(info);
            Assert.AreEqual("oBIcI", info.Id);
        }

        [Test]
        public void TestCreateAlbumAllParams()
        {
            ImgurAuthProvider authProvider = new AnonymousAuthenticationProvider(clientId);

            AlbumProvider albumProvider = new DefaultAlbumProvider(authProvider, baseTestUriTemplate);

            var expectedRequest = new TestableWebRequest(new Uri(baseTestUriTemplate));
            expectedRequest.EnqueueResponse(HttpStatusCode.OK, "Success", ResourceLoader.GetResourceText("Resources/AlbumProvider/AlbumModel.newcreate.json"), false);
            TestableWebRequestCreateFactory.GetFactory().AddRequest(expectedRequest);

            //AlbumProvider albumProvider = new DefaultAlbumProvider(authProvider.GetAuthorizationContext());

            IEnumerable<string> ids = new List<string>() { "MhjVogo", "EN2Tsxg", "n7AuG6d" };

            const string title = "The Title";
            const string description = "The Description";
            const string coverImageId = "n7AuG6d";

            AlbumInfo info = albumProvider.CreateAlbum(ids, title, description, PrivacyLevel.Public, LayoutStyle.Vertical, coverImageId);

            Assert.IsNotNull(info);
            Assert.AreEqual("oBIcI", info.Id);

            string kvTemplate = "Content-Disposition: form-data; name=\"{0}\"" + Environment.NewLine +
                                Environment.NewLine + "{1}" + Environment.NewLine;

            using (MemoryStream memoryStream = new MemoryStream(expectedRequest.GetContent()))
            {
                using (StreamReader reader = new StreamReader(memoryStream))
                {
                    string content = reader.ReadToEnd();
                    Assert.IsNotNull(content);

                    StringAssert.Contains( string.Format(kvTemplate, "title", title), content);
                    StringAssert.Contains(string.Format(kvTemplate, "description", description), content);
                    StringAssert.Contains(string.Format(kvTemplate, "cover", coverImageId), content);
                }
            }
        }

        [Test]
        [ExpectedException(typeof(ArgumentException))]
        public void TestNullIdsCreateAlbumBasic()
        {
            ImgurAuthProvider authProvider = new AnonymousAuthenticationProvider(clientId);

            AlbumProvider albumProvider = new DefaultAlbumProvider(authProvider, baseTestUriTemplate);

            var expectedRequest = new TestableWebRequest(new Uri(baseTestUriTemplate));
            expectedRequest.EnqueueResponse(HttpStatusCode.OK, "Success", ResourceLoader.GetResourceText("Resources/AlbumProvider/AlbumModel.newcreate.json"), false);
            TestableWebRequestCreateFactory.GetFactory().AddRequest(expectedRequest);

            AlbumInfo info = albumProvider.CreateAlbum(null, null, null, PrivacyLevel.Public, LayoutStyle.Blog, null);

        }

        [Test]
        [ExpectedException(typeof(ResponseException))]
        public void TestCreateAlbumFailResponse()
        {
            ImgurAuthProvider authProvider = new AnonymousAuthenticationProvider(clientId);

            AlbumProvider albumProvider = new DefaultAlbumProvider(authProvider, baseTestUriTemplate);

            var expectedRequest = new TestableWebRequest(new Uri(baseTestUriTemplate));
            expectedRequest.EnqueueResponse(HttpStatusCode.OK, "Success", ResourceLoader.GetResourceText("Resources/AlbumProvider/AlbumModel.newcreate.fail.json"), false);
            TestableWebRequestCreateFactory.GetFactory().AddRequest(expectedRequest);

            IEnumerable<string> ids = new List<string>() { "MhjVogo", "EN2Tsxg", "n7AuG6d" };

            AlbumInfo info = albumProvider.CreateAlbum(ids, null, null, PrivacyLevel.Public, LayoutStyle.Vertical, null);
        }
    }
}
